package _Trash;


import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.lucene.document.Field;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


public class BOMParser {

	public  ArrayList<org.apache.lucene.document.Document> allVerses = new ArrayList<org.apache.lucene.document.Document>();

	private boolean dbg = false;

	public   ArrayList<org.apache.lucene.document.Document>  parse (String fileName){

		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File(fileName)); 		//

			// normalize text representation
			doc.getDocumentElement ().normalize ();
			DBG("Root element of the doc is " + 
					doc.getDocumentElement().getNodeName());

			Element tstmt = doc.getDocumentElement();
			///NodeList rootChildren = tstmt.getChildNodes();//rmv

			// coverpg, titlepg, bookcoll, preface

			NodeList coverpgs = tstmt.getElementsByTagName("coverpg");
			NodeList titlepgs = tstmt.getElementsByTagName("titlepg");
			NodeList prefaces = tstmt.getElementsByTagName("preface");	
			NodeList bookcolls = tstmt.getElementsByTagName("bookcoll");

			int iCounter = 0;


			//coverpgs...
//			/				org.apache.lucene.document.Document currentDocument = new org.apache.lucene.document.Document();

			DBG("coverpg...");
			//assume 1 cpg:
			Node currentCoverpg = coverpgs.item(0);

			//coverpg title
			NodeList coverpgTitle = ((Element)currentCoverpg).getElementsByTagName("title");
			Node firstCoverpgTitle = coverpgTitle.item(0);
			String firstCoverpgTitleString = coverpgs.item(0).getTextContent();
			DBG("title "+firstCoverpgTitleString);	

			//coverpg title2
			NodeList coverpgTitle2 = ((Element)currentCoverpg).getElementsByTagName("title2");
			Node firstCoverpgTitle2 = coverpgTitle.item(0);
			String firstCoverpgTitle2String = coverpgs.item(0).getTextContent();
			DBG("title2 "+firstCoverpgTitle2String);	

			//coverpg subtitle
			NodeList coverpgSubTitles = ((Element)currentCoverpg).getElementsByTagName("subtitle");

			/*			for (int z = 0 ; z < coverpgSubTitle.getLength() ; z++) {
					String subtitleContents = coverpgSubTitle.item(z).getTextContent();
					DBG("\t\t\tv" + subtitleContents);
					currentDocument.add(new Field("subtitleContents", subtitleContents, Field.Store.YES, Field.Index.TOKENIZED));
					currentDocument.add(new Field("title2Contents", firstCoverpgTitle2String, Field.Store.YES, Field.Index.TOKENIZED));
					currentDocument.add(new Field("titleContents", firstCoverpgTitleString, Field.Store.YES, Field.Index.TOKENIZED)); //params??
				allVerses.add(iCounter, currentDocument);
				iCounter++;
				}*/
			String subtitleContents1String = coverpgSubTitles.item(0).getTextContent();			//remember
			String subtitleContents2String = coverpgSubTitles.item(1).getTextContent();



//			for (int k = 0 ; k < titlepgs.getLength() ; k++) {
//			/				org.apache.lucene.document.Document currentDocument2 = new org.apache.lucene.document.Document();

			DBG("titlepg...");
			Node currentTitlepg = titlepgs.item(0);

			//titlepg title
			NodeList titlepgTitle = ((Element)currentTitlepg).getElementsByTagName("title");
			Node firstTitilepgTitle = titlepgTitle.item(0);
			String firstTitlepgTitleString = titlepgs.item(0).getTextContent();				//remember
			DBG("title "+firstTitlepgTitleString);	

			//titlepg title2
			NodeList titlepgTitle2 = ((Element)currentTitlepg).getElementsByTagName("title2");
			//	for (int y = 0 ; y < titlepgTitle2.getLength() ; y++) {
			String firstTitlepgTitle2String = titlepgTitle2.item(0).getTextContent();	//remember
			//	DBG("\t\t\tv" + title2Contents);
			//	currentDocument2.add(new Field("title2Contents", title2Contents, Field.Store.YES, Field.Index.TOKENIZED));
			//	currentDocument2.add(new Field("titleContents", firstTitlepgTitleString, Field.Store.YES, Field.Index.TOKENIZED));
			//allVerses.add(iCounter, currentDocument2);
			//iCounter++;
			//	}
			//		}

			//preface...
//			for (int a = 0 ; a < prefaces.getLength() ; a++) {
			///		org.apache.lucene.document.Document currentDocument3 = new org.apache.lucene.document.Document();

			DBG("preface...");
			Node currentPreface = prefaces.item(0);

			//preface title
			NodeList prefaceTitle = ((Element)currentPreface).getElementsByTagName("ptitle");
			Node firstPrefaceTitle = prefaceTitle.item(0);
			String firstPrefaceTitleString = prefaces.item(0).getTextContent();			//remember
			DBG("preface title "+firstPrefaceTitleString);	

			//preface witlist
			NodeList witlist = ((Element)currentPreface).getElementsByTagName("witlist"); //list [2]

			NodeList firstWitnessList = ((Element) witlist.item(0)).getElementsByTagName("witness");
			NodeList secondWitnessList = ((Element) witlist.item(1)).getElementsByTagName("witness");

			String[] firstWitlistStrings = new String[firstWitnessList.getLength()];					//remember
			String[] secondWitlistStrings = new String[secondWitnessList.getLength()];					//remember

			for (int c = 0 ; c < firstWitnessList.getLength() ; c++) {
				firstWitlistStrings[c] = firstWitnessList.item(c).getTextContent();
			}
			for (int c = 0 ; c < secondWitnessList.getLength() ; c++) {
				secondWitlistStrings[c] = secondWitnessList.item(c).getTextContent();
			}



			Node bookcoll = bookcolls.item(0);
			Element firstBookcoll = (Element)bookcoll;	

			NodeList books = firstBookcoll.getElementsByTagName("book");

			//bookcoll..
			for (int d = 0 ; d < books.getLength() ; d++) {
				org.apache.lucene.document.Document currentDocument4 = new org.apache.lucene.document.Document();

				DBG("books...");															
				Node currentBook = books.item(d);

				//bktLong
				NodeList bktlongs = ((Element)currentBook).getElementsByTagName("bktlong");
				Node firstBktLong = bktlongs.item(0);
				String firstBktLongString = bktlongs.item(0).getTextContent();
				DBG("bktLong "+firstBktLongString);											

				//bktShort
				NodeList bktshort = ((Element)currentBook).getElementsByTagName("bktshort");
				Node firstBktShort = bktshort.item(0);
				String firstBktShortString = bktshort.item(0).getTextContent();				
				DBG("\tbktShort"+firstBktShortString);	

				//bksum
//				NodeList bkSum = ((Element)currentBook).getElementsByTagName("bksum");			//remember HANDLE this
//				Node firstBkSum = bkSum.item(0);
//				System.out.println(bkSum.getLength());
//				String firstBkSumString = bkSum.item(0).getTextContent();
//				DBG("\tbktShort"+firstBkSumString);	

				//chapters
				NodeList chapters = ((Element)currentBook).getElementsByTagName("chapter");
				for (int e = 0 ; e < chapters.getLength() ; e++) {

					Node firstChapter = chapters.item(e);

					NodeList chTitle = ((Element)firstChapter).getElementsByTagName("chtitle");
					DBG(chTitle.item(0).getTextContent());

					Node firstChTitle = chTitle.item(0);									
					String firstChTitleString =  firstChTitle.getTextContent(); 
					DBG("\t\tchTitle >> "+firstChTitle.getTextContent());

					NodeList chSum = ((Element)firstChapter).getElementsByTagName("chsum");
					DBG(chSum.item(0).getTextContent());

					Node firstChSum = chSum.item(0);									
					String firstChSumString =  firstChSum.getTextContent(); 			
					DBG("\t\tchSum >> "+firstChSum.getTextContent());				

					NodeList verses = ((Element)firstChapter).getElementsByTagName("v");
					for (int f = 0 ; f < verses.getLength() ; f++) {
						String vContents = verses.item(f).getTextContent(); 				//
						DBG("\t\t\tv" + vContents);

						currentDocument4.add(new Field("verseContents", vContents, Field.Store.NO, Field.Index.TOKENIZED)); //params??
						currentDocument4.add(new Field("chtitleContents", firstChTitleString, Field.Store.YES, Field.Index.TOKENIZED));
						currentDocument4.add(new Field("chsumContents", firstChSumString, Field.Store.YES, Field.Index.TOKENIZED));
						currentDocument4.add(new Field("chapterID", new Integer(e).toString(), Field.Store.YES, Field.Index.NO));
						currentDocument4.add(new Field("bktshortContents", firstBktShortString, Field.Store.YES, Field.Index.TOKENIZED));
						currentDocument4.add(new Field("bktlongContents", firstBktLongString, Field.Store.YES, Field.Index.TOKENIZED));
						currentDocument4.add(new Field("bookID",  new Integer(d).toString(), Field.Store.YES, Field.Index.NO));
						//bksum (?)
						System.out.println(d + " " + e + " " + f);
//						for (int wl = 0 ; wl < firstWitlistStrings.length ; wl++) {
//							currentDocument4.add(new Field("witness1", firstWitlistStrings[wl], Field.Store.YES, Field.Index.TOKENIZED));
//						}
//						for (int wl = 0 ; wl < secondWitlistStrings.length ; wl++) {
//							currentDocument4.add(new Field("witness2", secondWitlistStrings[wl], Field.Store.YES, Field.Index.TOKENIZED));
//						}
						currentDocument4.add(new Field("prefaceTitle",  firstPrefaceTitleString, Field.Store.YES, Field.Index.NO));
						currentDocument4.add(new Field("firstTitlepgTitle",  firstTitlepgTitleString, Field.Store.YES, Field.Index.NO));
						currentDocument4.add(new Field("secondTitlepgTitle",  firstTitlepgTitle2String, Field.Store.YES, Field.Index.NO));
						currentDocument4.add(new Field("firstSubtitle",  subtitleContents1String, Field.Store.YES, Field.Index.NO));
						currentDocument4.add(new Field("secondSubtitle",  subtitleContents2String, Field.Store.YES, Field.Index.NO));

						allVerses.add(iCounter,  currentDocument4);
						iCounter++;
					}
				}
			}
			System.out.println("end parsing");

		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}


		return allVerses;

	}


	public void DBG(String msg) {
		if(dbg) {
			System.out.println("DBG: "+msg);	
		}
	}

}

